<!--
 * @Author: 汪圣哲 wanghist061@163.com
 * @Date: 2024-03-02 17:54:21
 * @LastEditors: 汪圣哲 wanghist061@163.com
 * @LastEditTime: 2024-03-07 16:42:23
 * @FilePath: \my_backstage\src\views\Login.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <div class="container">
            <!-- 注册 -->
            <div class="container-form container-signup">
                <el-form ref="form" :model="register" status-icon :rules="rules" class="form" id="form1">
                    <h2 class="form-title">注册账号</h2>
                    <el-form-item prop="username" class="input">
                        <el-input autocomplete="off" placeholder="新账号"></el-input>
                    </el-form-item>

                    <el-form-item prop="password" class="input">
                        <el-input type="password" autocomplete="off" placeholder="新密码"></el-input>
                    </el-form-item>

                    <el-select v-model="value" placeholder="请选择身份" class="input" prop="identity">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>

                    <el-form-item>
                        <el-button class="btn btn2">点击注册</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <!-- 登录 -->
            <div class="container-form container-signin">
                <el-form ref="form" :model="login" status-icon :rules="rules" class="form" id="form2">
                    <h2 class="form-title">欢迎登录</h2>
                    <el-form-item prop="username" class="input">
                        <el-input v-model="login.username" autocomplete="off" placeholder="账号"></el-input>
                    </el-form-item>

                    <el-form-item prop="password" class="input">
                        <el-input type="password" v-model="login.password" autocomplete="off"
                            placeholder="密码"></el-input>
                    </el-form-item>
                    <el-select v-model="value" placeholder="请选择身份" class="input">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>

                    <el-form-item>
                        <el-button @click="submit" class="btn btn2">提交</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <!-- 叠加层部分 -->
            <div class="container-overlay">
                <div class="overlay">
                    <div class="overlay-panel overlay-left">
                        <button class="btn" id="signIn">
                            已有账号，直接登录
                        </button>
                    </div>
                    <div class="overlay-panel overlay-right">
                        <button class="btn" id="signUp">
                            没有账号，点击注册
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="background"></div>
    </div>
</template>

<script>
import Cookie from 'js-cookie'
import Mock from 'mockjs'
import { getMenu } from '../api/index'
export default {
    data() {
        return {
            // 登陆数据
            login: {
                username: '',
                password: '',
                identity: ''
            },
            // 注册数据
            register: {
                username: '',
                password: '',
                identity: ''
            },
            // 校验规则
            rules: {
                username: [{ required: 'true', message: '请输入账号', trigger: 'blur' }],
                password: [{ required: 'true', message: '请输入密码', trigger: 'blur' }],
            },
            options: [{
                value: '选项1',
                label: '学生'
            }, {
                value: '选项2',
                label: '教师'
            }, {
                value: '选项3',
                label: '管理员'
            }],
            value: ''
        }
    },
    mounted() {
        const signInButton = document.getElementById('signIn');
        const signUpButton = document.getElementById('signUp');
        const container = document.querySelector('.container');

        signInButton.addEventListener('click', () => {
            container.classList.remove('panel-active');
        });

        signUpButton.addEventListener('click', () => {
            container.classList.add('panel-active');
        });
    },
    methods: {
        submit() {
            // 表单的校验
            this.$refs.form.validate((valid) => {
                if (valid) {
                    // 传入表单数据
                    getMenu(this.login).then(({data}) => {
                        console.log(data)
                        if (data.code === 20000) {
                            // 记录cookie
                            Cookie.set('token', data.data.token)
                            // 设置菜单
                            this.$store.commit('SetMenu', data.data.menu)
                            // 动态添加路由
                            this.$store.commit('AddMenu', this.$router)
                            // 跳转到首页
                            this.$router.push('/home')
                        } else {
                            // 验证失败的弹窗
                            this.$message.error(data.data.message);
                        }
                    })
                } 
            })
        }
    }
}
</script>

<style scoped>
.background {
    position: fixed;
    /* 使背景图固定在页面上 */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("../assets/images/syids30.jpg") center no-repeat fixed;
    background-size: cover;
    filter: blur(3px);
    /* 模糊效果 */
    z-index: 1;
    /* 将背景置于内容之后 */
}

/* 主体 div 样式 */
.container {
    background-color: #e7e7e7;
    border-radius: 0.7rem;
    box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
        0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
    height: 420px;
    max-width: 750px;
    overflow: hidden;
    position: relative;
    width: 100%;
    z-index: 2;
    /* 使内容位于背景之上 */
    margin: 150px auto;
}

/* 登录、注册框部分 */
.container-form {
    height: 100%;
    position: absolute;
    top: 0;
    transition: all 0.6s ease-in-out;
}

/* 登录框 - 默认层级高 */
.container-signin {
    left: 0;
    width: 50%;
    z-index: 2;
}

/* 注册框 - 默认层级低 - 透明度 0 */
.container-signup {
    left: 0;
    opacity: 0;
    width: 50%;
    z-index: 1;
}

/* 表单样式 */
.form {
    background-color: #e7e7e7;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0 3rem;
    height: 100%;
    text-align: center;
}

.form-title {
    font-weight: 300;
    margin: 0;
    margin-bottom: 1.25rem;
}

.input {
    width: 100%;
    padding: 5px 10px;
}

.btn {
    background-color: #f25d8e;
    box-shadow: 0 4px 4px rgba(255, 112, 159, .3);
    border-radius: 5px;
    color: #e7e7e7;
    border: none;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: bold;
    letter-spacing: 0.1rem;
    padding: 0.9rem 4rem;
    text-transform: uppercase;
    transition: transform 80ms ease-in;
}

.btn2 {
    margin-top: 20px;
}

.form>.btn {
    margin-top: 1.5rem;
}

.btn:active {
    transform: scale(0.95);
}

/* ---------- 叠加部分样式 ------------- */
.container-overlay {
    height: 100%;
    left: 50%;
    overflow: hidden;
    position: absolute;
    top: 0;
    transition: transform 0.6s ease-in-out;
    width: 50%;
    z-index: 100;
}

.overlay {
    width: 200%;
    height: 100%;
    position: relative;
    left: -100%;
    background: url("../assets/images/syids30.jpg") no-repeat center fixed;
    background-size: cover;
    transition: transform 0.6s ease-in-out;
    transform: translateX(0);
}

.overlay-panel {
    height: 100%;
    width: 50%;
    position: absolute;
    top: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
}

.overlay-left {
    transform: translateX(-20%);
}

.overlay-right {
    right: 0;
    transform: translateX(0);
}

/* 设计激活时叠加层的位置 */
.panel-active .overlay-left {
    transform: translateX(0);
}

.panel-active .container-overlay {
    transform: translateX(-100%);
}

.panel-active .overlay {
    transform: translateX(50%);
}

/* 设置激活时，登录注册层的位置和透明度 */
.panel-active .container-signin {
    transform: translateX(100%);
}

.panel-active .container-signup {
    opacity: 1;
    z-index: 5;
    transform: translateX(100%);
}
</style>